package com.jieyang.oa.controller.person_manager;

import com.google.gson.Gson;
import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.entity.Position;
import com.jieyang.oa.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 29276
 * @version 1.0
 * @description: 岗位管理
 * @date 2021/8/15 14:55
 */
@WebServlet("/posServlet")
public class PosServlet extends BaseServlet {

    @Autowired
    private PositionService positionService;

    /**
     * @Author 2927624517@qq.com
     * @Description 岗位新增
     * @Date 2021/8/28 10:56
     * @Param [req, resp]
     * @return void
    **/
    public void posAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer posId = Integer.valueOf(req.getParameter("posId"));
        String posName = req.getParameter("posName");
        String posDesc = req.getParameter("posDesc");
        Position position = new Position();
        position.setPosId(posId);
        position.setPosName(posName);
        position.setPosDesc(posDesc);

        Integer insert = positionService.insert(position);
        if (insert == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询所有岗位
     * @Date 2021/8/28 10:56
     * @Param [req, resp]
     * @return void
    **/
    public void posList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        List<Position> positions = positionService.selectList();
//        for (Position position:positions) {
//            System.out.println(position.toString());
//        }
        resp.getWriter().write(new Gson().toJson(positions));
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 删除岗位
     * @Date 2021/8/28 10:56
     * @Param [req, resp]
     * @return void
    **/
    public void deletePos(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer posId = Integer.valueOf(req.getParameter("posId"));
        Integer delete = positionService.delete(posId);
        if (delete == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 更新岗位信息（查询岗位信息）
     * @Date 2021/8/28 10:57
     * @Param [req, resp]
     * @return void
    **/
    public void updatePos(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer posId = Integer.valueOf(req.getParameter("posId"));
        Position position = positionService.selectByPosId(posId);

        req.setAttribute("position", position);
        req.getRequestDispatcher("personManager/positionUpdate.jsp").forward(req, resp);
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 更新岗位信息（确认更新岗位信息）
     * @Date 2021/8/28 10:58
     * @Param [req, resp]
     * @return void
    **/
    public void posConfirmUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer posId = Integer.valueOf(req.getParameter("posId"));
        String posName = req.getParameter("posName");
        String posDesc = req.getParameter("posDesc");
        Position position = new Position();
        position.setPosId(posId);
        position.setPosName(posName);
        position.setPosDesc(posDesc);
        Integer update = positionService.update(position);

        if (update == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }
}
